# -*- encoding: utf-8 -*-
import xmltodict
import thread
from types import *
from datetime import datetime
#from peliculas.models import Movie


def parseXMLString():
	with open("/home/evida/Django-Projects/D11Films/peliculas/feed.xml","r") as myfile:
		data = myfile.read().replace('\n', '')
	parsed = xmltodict.parse(data)['Feed']
	count = 0
	for movie in parsed['Movie']:
		print movie['Title']
		print movie['Classification']['Name']
		print movie['Classification']['Adult']
		"""count = count + 1
		m = Movie()
		correct = True
		try:
			m.title = movie['Title']
		except (KeyError, TypeError):
			correct = False
		try:
			m.id_XML = movie['Id']
		except (KeyError, TypeError):
			correct = False
		try:
			m.plot = movie['Plot']
		except (KeyError, TypeError):
			correct = False
		try:
			m.publicWebUri = movie['PublicWebUri']
		except (KeyError, TypeError):
			correct = False
		try:
			modifiedDate = movie['LastModifiedDate']
			m.lastModifiedDate = datetime.strptime(modifiedDate[:21], '%Y-%m-%dT%I:%M:%S.%f')
			m.originalPublicationDate = datetime.now()
		except (KeyError, TypeError):
			correct = False
		try:
			originalPublicationDate = movie['OriginalPublicationDate']
			m.originalPublicationDate = datetime.strptime(modifiedDate[:21], '%Y-%m-%dT%I:%M:%S.%f')
		except (KeyError, TypeError):
			m.originalPublicationDate = datetime.now()
		try:
			m.artwork = movie['Artwork']
		except (KeyError, TypeError):
			correct = False
		try:
			m.classificationName = 'Rent'#movie['Classification']['Name']
			m.classificationAdult = 'false' #movie['Classification']['Adult']
		except (KeyError, TypeError):
			correct = False
		try:
			m.year = movie['Year']
		except (KeyError, TypeError):
			correct = False
		try:
			m.duration = movie['Duration']
		except (KeyError, TypeError):
			correct = False
		try:
			for genre in movie['Genres']['Genre']:
				pass
		except (KeyError, TypeError):
			pass
		try:
			ad = movie['Classification']['Adult']
			
		except (KeyError, TypeError):
			pass
		m.save()
	context = {'count':count, 'parsed':parsed}"""

#thread.start_new_thread( parseXMLString, (parsed, ) )

parseXMLString()



